package Controllers;

import Impl.OrderServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


@Controller
public class OrderAddShiXun2Controller {
	OrderServiceImpl orderService;

    @RequestMapping("/OrderAddShiXun2")
	public JSONObject userRegister(HttpServletRequest request, @RequestBody JSONObject in) throws Exception {
		ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
		orderService=(OrderServiceImpl)applicationContext.getBean("OrderServiceImpl");
		JSONObject obj = new JSONObject();
		String userid=(String)(request.getSession().getAttribute("userid"));
		String aString='"'+"";
		int class_id=Integer.parseInt(request.getParameter("class_id").replaceAll(aString, ""));
		int student_id=Integer.parseInt(request.getParameter("student_id").replaceAll(aString, ""));
		double class_price=Double.parseDouble(request.getParameter("class_price").replaceAll(aString, ""));
		try {
			orderService.AddOrderShiXun(userid, class_id, student_id, class_price);
			//request.getRequestDispatcher("index/orderlist.jsp").forward(request, response);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			request.getSession().setAttribute("errmsg", e.getMessage());
			obj.put("message", e.getMessage());
		}
		obj.put("status", "OK");
		return obj;
	}
//	protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		// TODO Auto-generated method stub
//    	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
//		orderService=(OrderServiceImpl)applicationContext.getBean("OrderServiceImpl");
//		String userid=(String)(request.getSession().getAttribute("userid"));
//		String aString='"'+"";
//		int class_id=Integer.parseInt(request.getParameter("class_id").replaceAll(aString, ""));
//		int student_id=Integer.parseInt(request.getParameter("student_id").replaceAll(aString, ""));
//		double class_price=Double.parseDouble(request.getParameter("class_price").replaceAll(aString, ""));
//		try {
//			orderService.AddOrderShiXun(userid,class_id,student_id,class_price);
//			request.getRequestDispatcher("index/orderlist.jsp").forward(request, response);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			request.getSession().setAttribute("errmsg", e.getMessage());
//			System.out.println(e.getMessage());
//			System.out.println("102030");
//			e.printStackTrace();
//		}
//	}
}
